package org.qq.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

import org.qq.clientthread.ClientLoginThread;
import org.qq.util.Util;
/**
 * 
 * project_name:QQ
 * package_name:org.qq.register
 * file_name:MainBoundary.java
 * @author cui&Link
 * create_date:2015年8月27日 下午5:39:44
 * modified_date:
 * description:群聊（登录进去的主界面）
 *
 */
public class GroupRoom extends JFrame{
	private JButton modifyButton,groupButton,privateChatButton;			//菜单栏按钮
	private JButton sendButton;											//发送消息按钮
	private JMenuBar menuBar;											//菜单栏
	private JComboBox faceBox;											//表情下拉框									
	private JTextField printField;										//消息文本框
	private JLabel faceLabel,friendLabel;								//表情标签
	private JTextArea textArea;											//消息区域
	private Socket s;													//套接字
	private ClientLoginThread clt;
	private JScrollPane areaPane,friendPane;							//消息框滚动条
	private String loginName;											//登录账号
	private DefaultTreeModel treeModel;
	private JTree jTree;
	private DefaultMutableTreeNode online,offline,root;
	private PrivateChatRoom privateChatRoom;
	
	public GroupRoom() {		//无參构造方法
		super();
	}
	
	
	/**
	 * 
	 * description: s,textArea,friendList,treeModel,jTree,online,offline,privateChatRoom的setter和getter方法
	 * creatTime:2015年9月3日上午11:57:18
	 * @author Link
	 * return type: Socket
	 *
	 */
	
	public PrivateChatRoom getPrivateChatRoom() {
		return privateChatRoom;
	}


	public void setPrivateChatRoom(PrivateChatRoom privateChatRoom) {
		this.privateChatRoom = privateChatRoom;
	}

	public Socket getS() {
			return s;
		}
	public void setS(Socket s) {
		this.s = s;
	}

	public DefaultMutableTreeNode getOnline() {
		return online;
	}

	public void setOnline(DefaultMutableTreeNode online) {
		this.online = online;
	}


	public DefaultMutableTreeNode getOffline() {
		return offline;
	}


	public void setOffline(DefaultMutableTreeNode offline) {
		this.offline = offline;
	}


	public JTextArea getTextArea() {
		return textArea;
	}

	public void setTextArea(JTextArea textArea) {
		this.textArea = textArea;
	}
	
	public DefaultTreeModel getTreeModel() {
		return treeModel;
	}
	

	public JTree getjTree() {
		return jTree;
	}


	public void setjTree(JTree jTree) {
		this.jTree = jTree;
	}
	public void setTreeModel(DefaultTreeModel treeModel) {
		this.treeModel = treeModel;
	}

	public  GroupRoom(Socket s,String loginName){
		
		this.s = s;
		this.loginName = loginName;
		setBackground(Color.GRAY);
		getContentPane().setBackground(Color.LIGHT_GRAY);
		this.setBounds(0, 0, 835, 600);
		this.setTitle("群聊界面");
		Util.screen(this);//调用Util包里面居中方法
		this.setDefaultCloseOperation(3);
		this.setResizable(false); 														// 不可改变大小
		getContentPane().setLayout(null);
		
		String []str = {"^_^","O(∩_∩)O"};
		faceBox = new JComboBox(str);									//表情框
		faceBox.setBackground(Color.ORANGE);
		faceBox.setBounds(62, 393,470,33);
		faceBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				printField.setText(printField.getText()+faceBox.getSelectedItem().toString());
			}
		});
		getContentPane().add(faceBox);
		
		faceLabel = new JLabel("表情");
		faceLabel.setForeground(Color.BLUE);
		faceLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		faceLabel.setBounds(10, 392, 51, 33);
		getContentPane().add(faceLabel);
		
		textArea = new JTextArea();
		textArea.setBounds(0, 4, 611, 383);
		textArea.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		textArea.setEditable(false);
		areaPane = new JScrollPane(textArea);
		areaPane.setBounds(0, 4, 611, 383);
		getContentPane().add(areaPane);
		
		
		friendLabel = new JLabel("好友列表");
		friendLabel.setBounds(670, 4, 100, 34);
		friendLabel.setFont(new Font("幼圆", Font.PLAIN, 20));
		friendLabel.setForeground(Color.BLUE);
		getContentPane().add(friendLabel);
		
		root = new DefaultMutableTreeNode("好友列表");
		online = new DefaultMutableTreeNode("在线好友列表");
		offline = new DefaultMutableTreeNode("离线好友列表");
		root.add(online);
		root.add(offline);
		jTree = new JTree(root);
		jTree.setRootVisible(false);
		treeModel = (DefaultTreeModel) jTree.getModel();
		jTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);//设置树的选择模式,一次只能选择一个
		jTree.addTreeSelectionListener(new TreeSelectionListener() {//设置树的监听事件
			@Override
			public void valueChanged(TreeSelectionEvent e) {
				String str = e.getPath().toString();//获取treepath路径并转换为字符串的形式
				//System.out.println(str);
				if(str.length()>15){
					String nameAndNickName = str.substring(15, str.length()-1);//获取用户名与昵称部分
					String name = str.substring(str.length()-10, str.length()-2);//获取账号部分
					if(name.equals(GroupRoom.this.getTitle())){//如果是本身就跳出
						return;
					}else{//否则new出私聊窗口
						privateChatRoom = new PrivateChatRoom(GroupRoom.this.s);
						privateChatRoom.setTitle(nameAndNickName);
						privateChatRoom.setVisible(true);
					}
				}
			}
		});
	
		friendPane = new JScrollPane(jTree);
		friendPane.setBounds(625, 40, 190, 383);
		getContentPane().add(friendPane);
		
		printField = new JTextField();
		printField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		printField.setBounds(10, 432, 534, 41);
		getContentPane().add(printField);
		printField.setColumns(10);
		printField.addKeyListener(new KeyListener() {//回车事件
			
			@Override
			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					if(!printField.getText().equals("")){
						String str1 = printField.getText()+"----groupChat";
						clt.sendMessageToServer(str1);//发送消息
						printField.setText("");//重置消息框的内容
					}
				}
			}
		});
		
		
		menuBar = new JMenuBar();
		menuBar.setBackground(Color.GRAY);
		setJMenuBar(menuBar);
		
		modifyButton = new JButton("用户修改");
		modifyButton.setForeground(new Color(0, 0, 0));
		modifyButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		modifyButton.setBackground(new Color(192, 192, 192));
		menuBar.add(modifyButton);
		modifyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//GroupRoom.this.dispose();			//关闭当前界面
				Modify c = new Modify(GroupRoom.this.getS(),GroupRoom.this.getTitle());			//将用户修改界面NEW出
				c.setVisible(true);
			}
		});
		
		groupButton = new JButton("群聊");
		groupButton.setForeground(Color.BLACK);
		groupButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		groupButton.setBackground(Color.PINK);
		menuBar.add(groupButton);
		groupButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null, "已在群聊界面界面");
			}
		});
		
		privateChatButton = new JButton("私聊");
		privateChatButton.setForeground(Color.BLACK);
		privateChatButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		privateChatButton.setBackground(Color.LIGHT_GRAY);
		menuBar.add(privateChatButton);
		
		clt = new ClientLoginThread(s);
		/**
		 * 发送消息按钮
		 */
		sendButton = new JButton("发送");
		sendButton.setBackground(Color.ORANGE);
		sendButton.setMargin(new Insets(0, 0, 0, 0));  		//无间距
		sendButton.setFont(new Font("幼圆", Font.PLAIN, 17));
		sendButton.setForeground(Color.BLACK);
		sendButton.setBounds(554, 426, 67, 50);
		getContentPane().add(sendButton);
		sendButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(!printField.getText().equals("")){
					String str1 = printField.getText()+"----groupChat";
					clt.sendMessageToServer(str1);//发送消息
					printField.setText("");//重置消息框的内容
				}
				
			}
		});
		this.addWindowListener(new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				clt.sendMessageToServer(GroupRoom.this.loginName+"----close");//发送用户下线消息
			}
			
			@Override
			public void windowClosed(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}

